<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ include file="import.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Action</title>
</head>
<body>

<%
	request.setCharacterEncoding("UTF-8");
	String content=request.getParameter("content");
	String username=String.valueOf(session.getAttribute("username"));
	int product_id=Integer.valueOf(request.getParameter("product_id"));
	String size=request.getParameter("size");
	Date date = new Date();
	String order_time=(1900+date.getYear())+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
	String order_close_time="";
	String stage="待处理";
	
	likeMapper likeMapper=sqlsession.getMapper(likeMapper.class);
	productMapper productMapper=sqlsession.getMapper(productMapper.class);
	carMapper carMapper=sqlsession.getMapper(carMapper.class);
	orderMapper orderMapper=sqlsession.getMapper(orderMapper.class);
	if(content.contentEquals("like")){
		likeMapper.insertLike(username, product_id);
	}else if(content.contentEquals("dislike")){
		likeMapper.updateLike(username, product_id);
	}else if(content.contentEquals("加入购物车")){
			List<car> car=carMapper.selectAllCarByUsernameSP(username,product_id,size);
			if(!car.isEmpty()){
				carMapper.updateCarNum(1,product_id,size);
			}else{
				carMapper.insertIntoCar(username, product_id, 1, size);
			}
		%><script>alert('加入购物车成功')</script><%
	}else if(content.contentEquals("立即购买")){
		List<product> product=productMapper.selectProductByID(product_id);
		for(product s:product){
			if(s.getStorage()>0){
				orderMapper.insertIntoOrder(username, product_id, size, order_time, order_close_time,stage);
				productMapper.updateProductSoldStorage(product_id, 1);
			}else{
				%><script>alert('商品无库存，请选择其他商品购买')</script><%
			}
		}
		%><script>alert('购买成功')</script><%
	}
	%><script>window.location.href="productInfo.jsp?product_id="+<%=product_id%>;</script><%
	sqlsession.commit();
	sqlsession.close();
%>

</body>
</html>